home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 2) (1997).iso / misc / coding / midas060 / src / mmem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-16  |  3.3 KB  |  150 lines

  1. /*      MMEM.C
  2.  *
  3.  * MIDAS Sound System memory handling routines
  4.  *
  5.  * $Id: mmem.c,v 1.4 1997/01/16 18:41:59 pekangas Exp $
  6.  *
  7.  * Copyright 1996,1997 Housemarque Inc.
  8.  *
  9.  * This file is part of the MIDAS Sound System, and may only be
  10.  * used, modified and distributed under the terms of the MIDAS
  11.  * Sound System license, LICENSE.TXT. By continuing to use,
  12.  * modify or distribute this file you indicate that you have
  13.  * read the license and understand and accept it fully.
  14. */
  15.  
  16. #include <stdlib.h>
  17.  
  18. #if defined(__WATCOMC__) || defined(__VC32__)
  19. #include <malloc.h>
  20. #else
  21. #ifdef __LINUX__
  22. #include <malloc.h>
  23. #else
  24. #include <alloc.h>
  25. #endif
  26. #endif
  27.  
  28. #include "lang.h"
  29. #include "errors.h"
  30. #include "mmem.h"
  31.  
  32. RCSID(const char *mmem_rcsid = "$Id: mmem.c,v 1.4 1997/01/16 18:41:59 pekangas Exp $";)
  33.  
  34.  
  35. /****************************************************************************\
  36. *
  37. * Function:     int memAlloc(unsigned len, void **blk);
  38. *
  39. * Description:  Allocates a block of conventional memory
  40. *
  41. * Input:        unsigned len            Memory block length in bytes
  42. *               void **blk              Pointer to memory block pointer
  43. *
  44. * Returns:      MIDAS error code.
  45. *               Pointer to allocated block stored in *blk, NULL if error.
  46. *
  47. \****************************************************************************/
  48.  
  49. int CALLING memAlloc(unsigned len, void **blk)
  50. {
  51. #ifdef DEBUG
  52.     unsigned    cnt, *b;
  53. #endif
  54.  
  55.     /* check that block length is not zero: */
  56.     if ( len == 0 )
  57.     {
  58.         ERROR(errInvalidBlock, ID_memAlloc);
  59.         return errInvalidBlock;
  60.     }
  61.  
  62. #ifdef DEBUG
  63.     len = (len + 3) & 0xFFFFFFFC;
  64.     cnt = len >> 2;
  65. #endif
  66.  
  67.     /* allocate memory: */
  68.     *blk = malloc(len);
  69.  
  70.     if ( *blk == NULL )
  71.     {
  72.         /* Memory allocation failed - check if heap is corrupted. If not,
  73.            assume out of memory: */
  74. #ifndef __LINUX__
  75. #if defined(__WATCOMC__) || defined(__VC32__)
  76.         if ( _heapchk() != _HEAPOK )
  77. #else
  78.         if ( heapcheck() != _HEAPOK )
  79. #endif
  80.         {
  81.             ERROR(errHeapCorrupted, ID_memAlloc);
  82.             return errHeapCorrupted;
  83.         }
  84.         else
  85. #endif
  86.         {
  87.             ERROR(errOutOfMemory, ID_memAlloc);
  88.             return errOutOfMemory;
  89.         }
  90.     }
  91.  
  92. #ifdef DEBUG
  93.     b = (unsigned*) *blk;
  94.     while ( cnt )
  95.     {
  96.         *(b++) = 0xDEADBEEF;
  97.         cnt--;
  98.     }
  99. #endif
  100.  
  101.     /* memory allocated successfully */
  102.     return OK;
  103. }
  104.  
  105.  
  106.  
  107. /****************************************************************************\
  108. *
  109. * Function:     int memFree(void *blk);
  110. *
  111. * Description:  Deallocates a memory block allocated with memAlloc()
  112. *
  113. * Input:        void *blk               Memory block pointer
  114. *
  115. * Returns:      MIDAS error code.
  116. *
  117. \****************************************************************************/
  118.  
  119. int CALLING memFree(void *blk)
  120. {
  121.     /* Check that block pointer is not NULL: */
  122.     if ( blk == NULL )
  123.     {
  124.         ERROR(errInvalidBlock, ID_memFree);
  125.         return errInvalidBlock;
  126.     }
  127.  
  128.     /* deallocate block: */
  129.     free(blk);
  130.  
  131.     /* deallocation successful */
  132.     return OK;
  133. }
  134.  
  135.  
  136. /*
  137.  * $Log: mmem.c,v $
  138.  * Revision 1.4  1997/01/16 18:41:59  pekangas
  139.  * Changed copyright messages to Housemarque
  140.  *
  141.  * Revision 1.3  1996/07/13 18:21:05  pekangas
  142.  * Fixed to compile with Visual C
  143.  *
  144.  * Revision 1.2  1996/05/24 16:20:36  jpaana
  145.  * Fixed for Linux
  146.  *
  147.  * Revision 1.1  1996/05/22 20:49:33  pekangas
  148.  * Initial revision
  149.  *
  150. */